package plenprojects.guiclient.panels;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ScrollPaneLayout;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import plenprojects.guiclient.frames.MailFrame;
import plenprojects.guiclient.frames.MainFrame;

public class MainPanel extends javax.swing.JPanel {
    
    private final MainFrame mainFrame;
    private final String id;
    
    private final JMenu roomMenu = new JMenu("Invite to chatroom");
    
    public MainPanel(MainFrame mainFrame, String id) {
        
        this.id = id;
        this.mainFrame = mainFrame;
        this.initComponents();
        
        
        this.createFriendsListPopupMenu();
        this.createChatRoomListPopupMenu();
    }
    
    public String getId() {
        
        return id;
    }
    
    private List<String> getUserChatRooms() {
        
        List<String> tmp = new ArrayList<>();
        
        tmp.add("trolo");
        tmp.add("tibia");
        tmp.add("rape");
        
        return tmp;
    }
    
    private void createFriendsListPopupMenu() {
        
        FriendsList.addMouseListener(new MouseListener() {

            private void check(MouseEvent e) {
                
                if (e.isPopupTrigger()) {
                    
                    FriendsList.setSelectedIndex(FriendsList.locationToIndex(e.getPoint()));
                    
                    JPopupMenu popup = new JPopupMenu();
                    for(String s : MainPanel.this.getUserChatRooms()) {
                        
                        popup.add(new JMenuItem(s));
                    }
                    popup.add(new JMenuItem((String)FriendsList.getSelectedValue()));
                    popup.show(FriendsList, e.getX(), e.getY());
                    
                }
            }
            
            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
                
                check(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                
                check(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        
        });
    }
    
    private void createChatRoomListPopupMenu() {
        
        ChatRoomsList.addMouseListener(new MouseListener() {

            private void check(MouseEvent e) {
                
                if (e.isPopupTrigger()) {
                    
                    ChatRoomsList.setSelectedIndex(ChatRoomsList.locationToIndex(e.getPoint()));
                    
                    JPopupMenu popup = new JPopupMenu();
                    
                    JMenuItem item = new JMenuItem("Join chatroom");
                    item.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                            
                            String room = (String)MainPanel.this.ChatRoomsList.getSelectedValue();
                            //MainPanel.this.creator.createNewFrame(room);
                        }
                        
                    });
                    
                    popup.add(item);
                    popup.show(ChatRoomsList, e.getX(), e.getY());
                    
                }
            }
            
            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mousePressed(MouseEvent e) {
                
                check(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                
                check(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        
        });
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jCheckBox1 = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        FriendsList = new javax.swing.JList();
        FriendsLabel = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ChatRoomsList = new javax.swing.JList();
        ChatRoomsLabel = new javax.swing.JLabel();
        FilterPanel = new javax.swing.JPanel();
        FilterLabel = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        RegexInput = new javax.swing.JTextField();
        OnlyPublicCheckbox = new javax.swing.JCheckBox();
        OnlyAvailableCheckBox = new javax.swing.JCheckBox();
        PrivateMessagesPanel = new javax.swing.JPanel();
        PrivateMessagesLabel = new javax.swing.JLabel();
        ShowMailButton = new javax.swing.JButton();
        NewMessagesLabel = new javax.swing.JLabel();

        jCheckBox1.setText("jCheckBox1");

        FriendsList.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        FriendsList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "dadas", "dada" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        FriendsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(FriendsList);

        FriendsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        FriendsLabel.setText("Friends");

        ChatRoomsList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(ChatRoomsList);

        ChatRoomsLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        ChatRoomsLabel.setText("ChatRooms");

        FilterPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        FilterLabel.setText("Filters");

        jLabel1.setText("Regex");

        RegexInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                RegexInputKeyReleased(evt);
            }
        });

        OnlyPublicCheckbox.setText("Show only public");
        OnlyPublicCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OnlyPublicCheckboxActionPerformed(evt);
            }
        });

        OnlyAvailableCheckBox.setText("Show only available");
        OnlyAvailableCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OnlyAvailableCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout FilterPanelLayout = new javax.swing.GroupLayout(FilterPanel);
        FilterPanel.setLayout(FilterPanelLayout);
        FilterPanelLayout.setHorizontalGroup(
            FilterPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(FilterPanelLayout.createSequentialGroup()
                .addComponent(FilterLabel)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(FilterPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(FilterPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(OnlyPublicCheckbox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(FilterPanelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(RegexInput, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(OnlyAvailableCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        FilterPanelLayout.setVerticalGroup(
            FilterPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(FilterPanelLayout.createSequentialGroup()
                .addComponent(FilterLabel)
                .addGap(18, 18, 18)
                .addGroup(FilterPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(RegexInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(OnlyPublicCheckbox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(OnlyAvailableCheckBox)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        PrivateMessagesPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        PrivateMessagesLabel.setText("Private messages");

        ShowMailButton.setText("Show mail");
        ShowMailButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ShowMailButtonActionPerformed(evt);
            }
        });

        NewMessagesLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        NewMessagesLabel.setText("You have 0 new messages");

        javax.swing.GroupLayout PrivateMessagesPanelLayout = new javax.swing.GroupLayout(PrivateMessagesPanel);
        PrivateMessagesPanel.setLayout(PrivateMessagesPanelLayout);
        PrivateMessagesPanelLayout.setHorizontalGroup(
            PrivateMessagesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PrivateMessagesPanelLayout.createSequentialGroup()
                .addComponent(PrivateMessagesLabel)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(PrivateMessagesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(NewMessagesLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PrivateMessagesPanelLayout.createSequentialGroup()
                .addContainerGap(72, Short.MAX_VALUE)
                .addComponent(ShowMailButton)
                .addGap(72, 72, 72))
        );
        PrivateMessagesPanelLayout.setVerticalGroup(
            PrivateMessagesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PrivateMessagesPanelLayout.createSequentialGroup()
                .addComponent(PrivateMessagesLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(NewMessagesLabel)
                .addGap(18, 18, 18)
                .addComponent(ShowMailButton)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(FriendsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addComponent(ChatRoomsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(FilterPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(PrivateMessagesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(FriendsLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ChatRoomsLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(FilterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(PrivateMessagesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
    }// </editor-fold>                        

    private void RegexInputKeyReleased(java.awt.event.KeyEvent evt) {                                       
        
        //TODO
        
    }                                      

    private void OnlyPublicCheckboxActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        
        //TODO
        
    }                                                  

    private void OnlyAvailableCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {                                                      
        
        //TODO
        
    }                                                     

    private void ShowMailButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
        
        //TODO
        
    }                                              

    // Variables declaration - do not modify                     
    private javax.swing.JLabel ChatRoomsLabel;
    private javax.swing.JList ChatRoomsList;
    private javax.swing.JLabel FilterLabel;
    private javax.swing.JPanel FilterPanel;
    private javax.swing.JLabel FriendsLabel;
    private javax.swing.JList FriendsList;
    private javax.swing.JLabel NewMessagesLabel;
    private javax.swing.JCheckBox OnlyAvailableCheckBox;
    private javax.swing.JCheckBox OnlyPublicCheckbox;
    private javax.swing.JLabel PrivateMessagesLabel;
    private javax.swing.JPanel PrivateMessagesPanel;
    private javax.swing.JTextField RegexInput;
    private javax.swing.JButton ShowMailButton;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration                   
}
